package com.jzmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.jzmy.cultivate.dao.CoustuQuestionsAnswersDao;
import com.jzmy.cultivate.entity.CoustuQuestionsAnswers;
import com.jzmy.cultivate.service.CoustuQuestionsAnswersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author mengzhichao
 * @create 2022-08-25-16:19
 */
@Service
public class CoustuQuestionsAnswersServiceImpl extends ServiceImpl<CoustuQuestionsAnswersDao, CoustuQuestionsAnswers> implements CoustuQuestionsAnswersService {
    @Override
    public Page<CoustuQuestionsAnswers> selectListPage(int current, int number, Map map) {
        // 新建分页
        Page<CoustuQuestionsAnswers> page = new Page<CoustuQuestionsAnswers>(current, number);
        return page.setRecords(this.baseMapper.dyGetList(page,map));
    }

    @Override
    public CoustuQuestionsAnswers findOneByKv(Object... params) {
        return getOne(this.convertToEntityWrapper(params));
    }


    private QueryWrapper<CoustuQuestionsAnswers> convertToEntityWrapper(Object... params) {
        QueryWrapper<CoustuQuestionsAnswers> ew = new QueryWrapper<>();
        if (params == null) {
            return ew;
        }
        for (int i = 0; i < params.length; i++) {
            if (i % 2 == 1 && params[i] != null && StringUtils.isNotBlank(params[i].toString())) {
                if(String.valueOf(params[i - 1]).equals("name")){
                    ew.like((String) params[i - 1], params[i].toString());
                }else if(String.valueOf(params[i - 1]).equals("file_name")){
                    ew.like((String) params[i - 1], params[i].toString());
                }else if(String.valueOf(params[i - 1]).equals("title")){
                    ew.like((String) params[i - 1], params[i].toString());
                }else if(String.valueOf(params[i - 1]).equals("introduce")){
                    ew.like((String) params[i - 1], params[i].toString());
                }else{
                    ew.eq((String) params[i - 1], params[i].toString());
                }
            }
        }
        return ew;
    }
}
